package dbcontrol

import (
	"MqttCloudProtocolServer/publiclib/datamodel/dborm"
)

//ModbusCommandsControl struct
type ModbusCommandsControl struct {
	base dbOperabase
	Data *dborm.Modbuscommands
}

//CreateModbusCommandsControl 初始化几种指针函数
func CreateModbusCommandsControl() *ModbusCommandsControl {
	content := new(ModbusCommandsControl)
	content.base.GetData = func() interface{} {
		return content.Data
	}
	content.base.SetData = func(data interface{}) {
		if data != nil {
			content.Data = data.(*dborm.Modbuscommands)
		}
	}
	content.base.GetKey = func() interface{} {
		if content.Data == nil {
			return nil
		}
		return content.Data.ID
	}
	content.base.CreateNew = func() interface{} {
		res := new(dborm.Modbuscommands)
		res.ID = 0
		return res
	}
	content.base.CreateNil = func() interface{} {
		return new(dborm.Modbuscommands)
	}
	return content
}

//GetAll 获取全部用户-服务器映射信息
func (_this *ModbusCommandsControl) GetAll() ([]*dborm.Modbuscommands, error) {
	res := make([]*dborm.Modbuscommands, 0)
	_, err := _this.base.GetAllByPage(&res, 0, 0, 0)
	return res, err
}

//CreateNew 创建一条记录
func (_this *ModbusCommandsControl) CreateNew() {
	_this.Data = _this.base.CreateNew().(*dborm.Modbuscommands)
}

//FindByID 根据主键获取一条记录
func (_this *ModbusCommandsControl) FindByID(id int) error {
	return _this.base.FindByID(id)
}

//Save 更新当前条目
func (_this *ModbusCommandsControl) Save() error {
	return _this.base.Save()
}

//Delete 删除当前条目
func (_this *ModbusCommandsControl) Delete() error {
	return _this.base.Delete()
}

//GetByClientName 根据客户机名称查询
func (_this *ModbusCommandsControl) GetByUUID(uuid string) ([]*dborm.Modbuscommands, error) {
	res := make([]*dborm.Modbuscommands, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "UUID = ?", uuid)
	return res, err
}
